package com.sghd.fight.module.game.config;

import com.sghd.common.ramcache.IEntity;
import com.sghd.common.ramcache.PerPersist;
import com.sghd.common.ramcache.anno.Cached;
import com.sghd.common.ramcache.anno.CachedSizes;
import com.sghd.common.ramcache.anno.Persister;
import com.sghd.common.ramcache.anno.Persisters;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.fight.game.ChessType;

import javax.persistence.*;
import java.util.HashMap;
import java.util.Map;

/**
 * @author frank
 * @date 2019-03-20 15:09
 * @desc
 **/
@Entity
@Table(catalog = "bsdld_fight")
@Cached(size = CachedSizes.DEFAULT, persister = @Persister(Persisters.PRE_MINUTE))
public class RobotConfig implements IEntity<Integer>, PerPersist {
    @Id
    private int id;
    /** 难度*/
    @Enumerated(EnumType.STRING)
    private RobotType type;
    /** 五行等级*/
    @Transient
    private Map<ChessType, Integer> element2Level;
    private String element2LevelJson;
    /** 技能对应等级*/
    @Transient
    private Map<Integer, Integer> skill2Level;
    private String skill2LevelJson;
    /** 初始魔法值*/
    @Transient
    private Map<ChessType, Integer> initMagic;
    private String initMagicJson;

    @Override
    public void afterLoad() {
        element2Level = JsonUtils.string2Map(element2LevelJson, ChessType.class, Integer.class);
        skill2Level = JsonUtils.string2Map(skill2LevelJson, Integer.class, Integer.class);
        initMagic = JsonUtils.string2Map(initMagicJson, ChessType.class, Integer.class);
    }

    @Override
    public void beforePersist() {
        element2LevelJson = JsonUtils.object2String(element2Level);
        skill2LevelJson = JsonUtils.object2String(skill2Level);
        initMagicJson = JsonUtils.object2String(initMagic);
    }

    @Override
    public Integer getId() {
        return id;
    }

    void setId(int id) {
        this.id = id;
    }

    public RobotType getType() {
        return type;
    }

    void setType(RobotType type) {
        this.type = type;
    }

    public Map<ChessType, Integer> getElement2Level() {
        return element2Level;
    }

    void setElement2Level(Map<ChessType, Integer> element2Level) {
        this.element2Level = element2Level;
    }

    public Map<Integer, Integer> getSkill2Level() {
        return skill2Level;
    }

    void setSkill2Level(Map<Integer, Integer> skill2Level) {
        this.skill2Level = skill2Level;
    }

    public Map<ChessType, Integer> getInitMagic() {
        return initMagic;
    }

    void setInitMagic(Map<ChessType, Integer> initMagic) {
        this.initMagic = initMagic;
    }

    public String getElement2LevelJson() {
        return element2LevelJson;
    }

    void setElement2LevelJson(String element2LevelJson) {
        this.element2LevelJson = element2LevelJson;
    }

    public String getSkill2LevelJson() {
        return skill2LevelJson;
    }

    void setSkill2LevelJson(String skill2LevelJson) {
        this.skill2LevelJson = skill2LevelJson;
    }

    public String getInitMagicJson() {
        return initMagicJson;
    }

    void setInitMagicJson(String initMagicJson) {
        this.initMagicJson = initMagicJson;
    }

    public RobotConfig copy() {
        RobotConfig config = new RobotConfig();
        config.id = this.id;
        config.initMagic = new HashMap<>(this.initMagic);
        config.skill2Level = new HashMap<>(skill2Level);
        config.element2Level = new HashMap<>(element2Level);
        config.type = this.type;
        return config;
    }
}
